package compro.admission.form.email;

import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public abstract class EmailTemplate {
	private Email email;
	private String from;
	private Session session;
	
	public EmailTemplate(Email email){
		this.email = email;
	}
	public abstract void setEmailContent();
	public void configEmail(){

		// Assuming you are sending email from localhost
		String password = "aptech2me4ever";
		String host = "smtp.gmail.com";
		from = "sample4students@gmail.com";
		
		// Get system properties
		Properties properties = System.getProperties();
		properties.setProperty("mail.smtp.host", host);
		properties.setProperty("mail.smtp.user", from);
		properties.setProperty("mail.smtp.password", password);
		properties.setProperty("mail.smtp.port", "587");
		properties.setProperty("mail.smtp.auth", "true");
		properties.setProperty("mail.smtp.starttls.enable", "true");
		

		// Get the default Session object.
		session = Session.getInstance(properties,
				new GMailAuthenticator(from, password));
		
	}
	public void sendEmail() {
		
		configEmail();
		try {

			// Create a default MimeMessage object.
			MimeMessage eMessage = new MimeMessage(session);

			// Set From: header field of the header.
			eMessage.setFrom(new InternetAddress(from));
			
			
			// Set To: header field of the header.
			eMessage.addRecipient(javax.mail.Message.RecipientType.TO,
					new InternetAddress(email.getToEmail()));

			// Set Subject: header field
			eMessage.setSubject(email.getSubject());

			// Now set the actual message
			eMessage.setText(email.getMailText());
			

			// Send message
			Transport.send(eMessage);
		}

		catch (MessagingException msg) {
			msg.printStackTrace();
		}
		
	}
	public Email getEmail() {
		return email;
	}
	public void setEmail(Email email) {
		this.email = email;
	}
	
	
}
